﻿#include "stdafx.h"
#include "XComboIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "Util.h"
#include "IXCombo.h"
#include "IXEnumConfigNode.h"

#include "ParseHelper.h"


CXComboIntParser::CXComboIntParser() : CXIntParserBase(&TIntCreateControl<IXCombo>)
{
    ;
}

CXComboIntParser::~CXComboIntParser()
{
    ;
}

// 创建控件之前
BOOL CXComboIntParser::PreCreate(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    if(!m_ParentParser.PreCreate(pNode, pControl))
        return FALSE;

    IXCombo* pCombo = Util::QueryIntf<IXCombo>(pControl);
    if(pCombo == NULL)
        return FALSE;

    XBSTR bstrStyle;

    // CurSel
    if(SUCCEEDED(pNode->GetValue(_T("style"), &bstrStyle)))
    {
        struct stStyleInfo
        {
            LPCTSTR szStyleName;
            DWORD   dwStyle;
        } data[] =
        {
            _T("simple"),       CBS_SIMPLE,
            _T("dropdown"),     CBS_DROPDOWN,
            _T("dropdownlist"), CBS_DROPDOWNLIST,
        };
        XString strStyle(bstrStyle);
        size_t handle = 0;
        XString strToken;
        while(StrUtil::NextToken(strStyle, handle, _T(";"), strToken, FALSE))
        {
            for(int i=0; i<_countof(data); ++ i)
            {
                if(_tcsicmp(data[i].szStyleName, strToken.GetData()) == 0)
                {
                    pCombo->ModifyStyle(data[i].dwStyle, 0);
                    break;
                }
            }
        }
    }

    return TRUE;
}

// 填充属性
BOOL CXComboIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;
    DWORD dwTemp;
    LONG nTemp;

    IXCombo* pCombo = Util::QueryIntf<IXCombo>(pControl);
    if(pCombo == NULL)
        return FALSE;

    // 数据
    int nIndex;
    XPtr<IXEnumConfigNode> pEnum;
    if(SUCCEEDED(pNode->EnumNode(_T("\\proplist"), _T("Item"), (IXEnum**)&pEnum)))
    {
        IXConfigNode* pPropNode = NULL;
        while(S_OK == (pEnum->Next(&pPropNode)))
        {
            // Text
            if(SUCCEEDED(pPropNode->GetValue(_T("string"), &bstrTemp)))
            {
                // ItemData
                nIndex = pCombo->AddString(bstrTemp);
                if(SUCCEEDED(pPropNode->GetValue(_T("data"), &dwTemp)))
                {
                    pCombo->put_ItemData(nIndex, dwTemp);
                }
            }
            XSAFE_RELEASE(pPropNode);
        }
        XSAFE_RELEASE(pPropNode);
    }

    // CurSel
    if(SUCCEEDED(pNode->GetValue(_T("curSel"), &nTemp)))
    {
        pCombo->put_CurSel(nTemp);
    }

    // MinVisibleItemCount
    if(SUCCEEDED(pNode->GetValue(_T("minVisible"), &nTemp)))
    {
        pCombo->put_MinVisibleItemCount(nTemp);
    }

    return bResult;
}
